<?php
    class Online_Model extends CI_Model
    {
        var $table_sub = "db_sub_online";
        var $table = "db_users_online";
        function __construct()
        {
            parent::__construct();
        }
        function list_sub_online()
        {
            return $this->db->get($this->table_sub)->result();
        }
        function list_online()
        {
            return $this->db->get($this->table)->result();
        }
        function add_group_online($data)
        {
            $data = array(
                'name'=>$data['name'],
                'description' => $data['description'],
                'order_sub' => $data['order_sub'],
                'active'    => $data['active']
            );
            $this->db->set($data);
            if($this->db->insert($this->table_sub)) return true; else return false;
        }
        function update_group_online($id,$data)
        {
            $this->db->where('id_sub',$id);
            $data = array(
                'name'=>$data['name'],
                'description' => $data['description'],
                'order_sub' => $data['order_sub'],
                'active'    => $data['active']
            );
            $this->db->set($data);
            if($this->db->update($this->table_sub)) return true; else return false;
        }
        function add_user_online($data)
        {
            $data = array(
                'id_sub'=>$data['id_sub'],
                'phone' => $data['phone'],
                'yahoo' => $data['yahoo'],
                'skype'    => $data['skype'],
                'order_users'    => $data['order_users'],
                'active'    => $data['active'],
                'description'    => $data['description']
            );
            $this->db->set($data);
            if($this->db->insert($this->table)) return true; else return false;
        }
        function update_user_online($id,$data)
        {
            $this->db->where('id_user',$id);
            $data = array(
                'id_sub'=>$data['id_sub'],
                'phone' => $data['phone'],
                'yahoo' => $data['yahoo'],
                'skype'    => $data['skype'],
                'order_users'    => $data['order_users'],
                'active'    => $data['active'],
                'description'    => $data['description']
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        
        function update_group_active($id,$data)
        {
            $this->db->where('id_sub',$id);
            $data = array(
                'active' => $data['active']
            );
            $this->db->set($data);
            if($this->db->update($this->table_sub)) return true; else return false;
        }
        function update_users_active($id,$data)
        {
            $this->db->where('id_user',$id);
            $data = array(
                'active' => $data['active']
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        function get_info_sub($id)
        {
            $this->db->where('id_sub',$id);
            return $this->db->get($this->table_sub)->row();    
        }
        function get_info_nick($id)
        {
            $this->db->where('id_user',$id);
            return $this->db->get($this->table)->row();    
        }
        function delete_group_online($id)
        {            
            $this->db->where('id_sub',$id);
            return $this->db->delete($this->table_sub);
        }
        function delete_nick_online($id)
        {
            $this->db->where('id_user',$id);
            return $this->db->delete($this->table);
        }
        function get_name_group_online($id)
        {
            $q = $this->db->get_where($this->table_sub,array('id_sub'=>$id));
            if($q->num_rows () > 0)
            {
                return $q->row()->name;
            }
            else{
                return false;
            }
        }
    }
?>